const db = require('../config/db.js');

const collectModel = {
    getCollectList:async ({user_id,pageCurrent,CarKeyword}) => {
        console.log(pageCurrent,'当前页');
        let current = (pageCurrent - 1) * 10 || 0;
        let size = 10;
        let sql1  = 'SELECT COUNT(DISTINCT collect.car_id) AS total FROM collect JOIN car ON collect.car_id = car.id  WHERE  collect.user_id = ?';
        let sql2 = `SELECT COUNT(DISTINCT collect.car_id) AS total FROM collect JOIN car ON collect.car_id = car.id WHERE collect.user_id = ? AND  CONCAT(car.brand,car.model) LIKE CONCAT('%', ?, '%');`
        let sql3 = `SELECT collect.id AS id, collect.user_id AS user_id,collect.car_id AS car_id,collect.state AS state,car.brand AS brand,car.model AS model,collect.collect_time AS collect_time,info,image FROM collect JOIN car ON collect.car_id = car.id  WHERE collect.user_id = ?  GROUP BY collect.id, collect.user_id, collect.car_id, collect.state, car.brand, car.model, collect.collect_time, info, image LIMIT ${current},${size};`;
        let sql4 = `SELECT collect.id AS id, collect.user_id AS user_id,collect.car_id AS car_id,collect.state AS state,car.brand AS brand,car.model AS model,collect.collect_time AS collect_time, info,image FROM collect JOIN car ON collect.car_id = car.id WHERE collect.user_id = ? AND  CONCAT(car.brand,car.model) LIKE CONCAT('%', ?, '%') GROUP BY collect.id, collect.user_id, collect.car_id, collect.state, car.brand, car.model, collect.collect_time, info, image LIMIT ${current},${size};`
        try {
            if(CarKeyword) {
                const res1 = await db.query(sql4, [user_id,CarKeyword]);
                const res2 = await db.query(sql2,[user_id,CarKeyword]);
                const [{total}]= res2[0];
                console.log(total, '搜索当前用户收藏产品列表总记录');
                console.log(res1[0], '搜索当前用户收藏产品列表分页带模糊查询');
                return {
                    list: res1[0],
                    total
                }
            }else{
                const res1 = await db.query(sql1,[user_id]);
                const res2 = await db.query(sql3,[user_id]);
                const [{total}]= res1[0];
                console.log(total, '获取当前用户收藏列表总记录');
                console.log(res2[0], '获取当前用户收藏列表记录分页');
                return {
                    total,
                    list: res2[0]
                }
            }
        }catch (error){
            console.log(error,'CollectModel层 获取用户收藏列表出错啦');
        }
    },
    isCollect:async ({user_id,car_id}) => {
        console.log(user_id,car_id,'是否收藏入参');
        const sql = "SELECT * FROM collect WHERE user_id=? AND car_id=?";
        try {
            // state为 2 表示当前用户未收藏
            if(user_id && car_id){
                const res = await db.query(sql,[user_id,car_id]);
                if(res[0].length > 0){
                    return 1
                }else{
                    return 0;
                }
            }else{
                return null;
            }
        }catch (error) {
            console.log(error,'添加收藏model层出错啦');
        }
    },
    addCollect:async ({user_id,car_id,state,collect_time}) => {
        console.log(user_id,'添加收藏入参');
        const sql1 = 'INSERT INTO collect (user_id,car_id,state,collect_time) VALUES (?,?,?,?);';
        const sql2 = 'DELETE FROM collect WHERE user_id= ? AND car_id = ?;';
        try {
            // state为 2 表示当前用户未收藏
            if(state === 2){
                const res = await db.query(sql1,[user_id,car_id,state,collect_time]);
                console.log(res,'model层收藏');
                return 1;
            }else{
                const res = await db.query(sql2,[user_id,car_id]);
                return 0;
            }
        }catch (error) {
            console.log(error,'添加收藏model层出错啦');
        }
    },
    deleteCollect:async ({id}) => {
        console.log(id,'删除收藏入参');
        const sql =  'DELETE FROM collect WHERE id = ?;'
        try {
            if(!!id){
                const res = await db.query(sql,[id]);
                return 1
            }else{
                return 0
            }
        }catch (error) {
            console.log(error,'删除收藏model层出错啦');
        }
    },
};

module.exports = collectModel;